﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using System.Windows;
using System.Configuration;

namespace EmailHelper.tool
{
    public class AllEmailHelper
    {
        public static String authCode = ConfigHelper.GetConfig("authCode");
        public static int serverPort = int.Parse(ConfigHelper.GetConfig("serverPort"));
        public static String SMTPPath = ConfigHelper.GetConfig("SMTPPath");
        public static Boolean sendEmail(List<String> toemails, String ccpath, String fromEmail, String emailTitle, String emailContent,String FilePath)
        {
            MailMessage message = new MailMessage();
            try
            {
                foreach (String item in toemails)
                {
                    message.To.Add(item);
                }
            }
            catch
            {
                MessageBox.Show("收件人输入错误", "提示");
                return false;
            }
            try
            {
                if(ccpath!=null)
                    message.CC.Add(ccpath);
            }
            catch
            {
                MessageBox.Show("抄送输入错误", "提示");
            }
            //添加附件
            message.Attachments.Add(new Attachment(FilePath));

            message.From = new MailAddress(fromEmail);

            message.Subject = emailTitle;
            message.Body = emailContent;

            message.BodyEncoding = Encoding.UTF8;

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = SMTPPath;
            smtpClient.Port = serverPort;

            smtpClient.EnableSsl = true;

            smtpClient.Credentials = new System.Net.NetworkCredential(fromEmail, authCode);
            try
            {
                smtpClient.SendAsync(message,null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
                return false;
            }

            return true;
        }

        
    }
}
